'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('Zhekou1List');

export const GET_LIST_DONE = ns('GET_LIST_DONE');
export const EDIT_ACTIVE = ns('EDIT_ACTIVE');
export const REMOVE_ACTIVE = ns('REMOVE_ACTIVE');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');

export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function removeActive(activity_id) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.activity.delete',
					param : {activity_id},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'removeLimitActive',
				  method: 'GET',
				  data:{activity_id}
				},(json) => {
				  //成功后，在数据库中删除活动
					ajax({
						  api: 'removeLimitFromDatabase',
						  method: 'GET',
						  data:{activity_id}
						},(json) => {
							dispatch({type: REMOVE_ACTIVE,activity_id});
						},(json)=>{
							const login = (json.code==210);
							dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:login
							});
					});

				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
					//json.code==15 && json.sub_code:"isv.activity-not-exist"  折扣不存在，需要提示从数据库中删除活动
					if(json.code==15 && json.sub_code=="isv.activity-not-exist" ){
						ajax({
							  api: 'removeLimitFromDatabase',
							  method: 'GET',
							  data:{activity_id}
							},(json) => {
								dispatch({type: REMOVE_ACTIVE,activity_id});
							},(json)=>{
								const login = (json.code==210);
								dispatch({
									type: LOAD_ERR,
									message:json.msg+(json.sub_msg?json.sub_msg:""),
									loginFalse:login
								});
						});
					}
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function editActive(params) {
	if(params.start_time instanceof Date){
		const ts = params.start_time;
		params.startTime = (ts.getFullYear()+"-"+(ts.getMonth()+1)+"-"+ts.getDate()+" "+ts.getHours()+":"+ts.getMinutes()+":"+ts.getSeconds() );
	}
	if(params.end_time instanceof Date){
		const ts = params.end_time;
		params.end_time = (ts.getFullYear()+"-"+(ts.getMonth()+1)+"-"+ts.getDate()+" "+ts.getHours()+":"+ts.getMinutes()+":"+ts.getSeconds() );
	}
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START,
		  data: params
		});
		if(window.workbench){
			//千牛环境执行
			  window.QN.top.invoke( {
					cmd : 'taobao.promotionmisc.common.item.activity.update',
					param : params,
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			ajax({
				  api: 'editRingLimitActive',
				  method: 'GET',
				  data:params
				},(json) => {
				  //成功后，更新数据库中的信息
					if(params.start_time instanceof Date){
						params.startTime = Date.parse(params.start_time)/1000;
					}else{
						params.startTime = Date.parse(new Date(params.start_time))/1000;
					}
					if(params.end_time instanceof Date){
						params.endTime = Date.parse(params.end_time)/1000;
					}else{
						params.endTime = Date.parse(new Date(params.end_time))/1000;
					}
					params.act_id = params.activity_id;
					params.tagName = params.name;
					params.notice = params.description;
					ajax({
						  api: 'saveLimitToDatabase',
						  method: 'GET',
						  data:params
						},(json) => {
							dispatch({
							  type: EDIT_ACTIVE,
							  data: params
							});
						},(json)=>{
							const login = (json.code==210);
							  dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:login
							  });
						});

				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function getList(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.common.item.activity.list.get',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		ajax({
			  api: 'getLimitListInDatabase',
			  method: 'GET',
			  data:params
			},(json) => {
			  //成功后
				if(json.activity_list && !(json.activity_list.common_item_activity instanceof Array)){
					json.activity_list.common_item_activity = Array(json.activity_list.common_item_activity);
				}
				if(!json.activity_list){
					json.activity_list={common_item_activity:[]};
				}
			    dispatch({
				  type: GET_LIST_DONE,
				  data: json
				});

			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}


